﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Meeting.Public;
using Meeting.Model;

namespace Meeting.Web.Admin.BaseSet
{
    /// <summary>
    /// EmployeeOperation 的摘要说明
    /// </summary>
    public class RoomOperation : IHttpHandler
    {
        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 20;
        BLL.Room bll = new BLL.Room();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";
            //context.Response.ContentType = "text/plain";
            methodName = context.Request.Params["method"];
            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);

            switch (methodName.ToLower())
            {
                case "getrooms":   //获取json数据
                    context.Response.Write(bll.GetPage(nPageIndex, nPageSize, " 1=1 "));
                    break;
                case "getbyid":   //获取json数据
                    context.Response.Write(bll.GetBy(CommonFunction.varToIntDef(context.Request.Params["param"], 0)));
                    break;
                case "save":
                     SaveData(context) ;
                    break;
                case "delete":
                    context.Response.Write(DeleteData(context));
                    break;
            }
        }

        /// <summary>
        /// 保存会议室
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private void SaveData(HttpContext context)
        {
            Meeting.Model.Room model = new Model.Room();
            model.Id = CommonFunction.varToIntDef(context.Request.Params["id"], 0);
            model.Name = CommonFunction.removeAttackOfSql(context.Request.Params["name"]);
            model.Position = CommonFunction.removeAttackOfSql(context.Request.Params["position"]); 
            model.PeopleCount = CommonFunction.varToIntDef(context.Request.Params["peopleCount"], 0); 
            model.Comment = CommonFunction.removeAttackOfSql(context.Request.Params["comment"]); 
            //
            Model.JsonResponse res = new Model.JsonResponse(); 
            if ((model.Id == 0 ? bll.Add(model ) > 0 : bll.Update(model)))
            {
                res.success = true;
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            context.Response.Write(JsonConvert.SerializeObject(res));
        }

        //删除会议室
        private string DeleteData(HttpContext context)
        {
            string ids = context.Request["id"];
            //提交数据
            Model.JsonResponse rsp = new Model.JsonResponse();

            if (bll.DeleteList(ids))
            {
                rsp.success = true;
            }
            else
            {
                rsp.success = false;
                rsp.Message = bll.errorString;
            }
            return JsonConvert.SerializeObject(rsp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}